package cn.oyjg.sys.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.oyjg.base.controller.BaseController;
import cn.oyjg.base.util.DateFormater;
import cn.oyjg.base.util.ResultFlag;
import cn.oyjg.sys.entity.GameInfo;
import cn.oyjg.sys.entity.GameLog;
import cn.oyjg.sys.entity.User;
import cn.oyjg.sys.service.IGameInfoService;
import cn.oyjg.sys.service.IGameLogService;
import cn.oyjg.sys.service.IUserService;

/**
 * 处理前台用户的请求数据
 * @author k11ha_000
 *
 */
@Controller
public class GameUserController extends BaseController{
	Logger log=Logger.getLogger(GameUserController.class);

	@RequestMapping(value = "c_login")
	@ResponseBody
	public ResultFlag login(@RequestParam String uname, @RequestParam String pwd) {
		ResultFlag rf=new ResultFlag();
		try {
			User user = uSvr.login(uname, pwd,2);
			if (user != null) {
				rf.setFlag(true);
				//计算游戏时间
				uSvr.calcGameTim(user);
				rf.setData(user);
			} else {
				rf.setFlag(false);
				rf.setMsg("用户名或密码错误");
			}
		} catch (Exception err) {
			log.error("系统异常:"+err.getMessage());
			rf.setFlag(false);
			rf.setMsg("系统异");
		}
		return rf;
	}
	
	/**
	 * 游戏启动，插入游戏记录
	 */
	@RequestMapping(value="c_addGameInfo")
	public void addGameInfo(Integer userid,String username,String truename){
		GameLog gl=new GameLog();
		gl.setUserid(userid);
		gl.setUsername(username);
		gl.setTruename(truename);
		gl.setStarttime(DateFormater.getDateFormat("yyyy-MM-dd HH:mm"));
		try {
			gameLogSvrSvr.insert(gl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取游戏信息列表,只返回进程名称的拼接
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "c_getAllGameInfo")	
	public void getGameList() throws IOException{
		String s="";
		List<GameInfo> list=new ArrayList<GameInfo>();
		list=gameInfoSvr.selectAll();
		for(GameInfo info :list){
			s+=info.getProcessname()+",";
		}
		if(s.length()>1){
			s=s.substring(0,s.length()-1);
		}
		response.getWriter().print(s);
	}
	
	@Resource(name = "userService")
	private IUserService uSvr;
	
	@Resource(name = "gameInfoService")
	private IGameInfoService gameInfoSvr;
	
	@Resource(name = "gameLogService")
	private IGameLogService gameLogSvrSvr;
}
